#include "AnaDef.h"
#include "XGLUtils.h"
#include "style.h"

void neweff(TString ilist, const Double_t mom /*, const Double_t nclsNch, */)
{
  TChain *ch=XGLUtils::InputFiles(ilist,"tree");
 
  const Double_t dp = 0.1;
  const Int_t nch=6;
  const Int_t ntpc = 120;
  const Double_t minncls = 17;
  const Double_t maxncls = 22;//by observation

  TList *ll = new TList;

  /*
 //in the tender it is the status bits of the tracks 14 - 16 for electrons, pions and protons
    Int_t tmpbit = 0;
    if(esdtrack->TestBit(BIT(14))) tmpbit+=1;
    if(esdtrack->TestBit(BIT(15))) tmpbit+=2;
    if(esdtrack->TestBit(BIT(16))) tmpbit+=4;
  pidV0 = tmpbit;

   */

  //================================================================>>
  //TRD
  const TString TRDcut = Form("trdnch>=%d && trdncls/trdnch>%.3f && tpcncls>%d && pTRD<%f && pTRD>%f", nch, minncls, ntpc, mom+dp, mom-dp);

  printf("TRDcut: %s\n", TRDcut.Data());
  TString tmpcut;

  //--

  tmpcut = TRDcut+"&& pidV0==1";
  printf("tmpcut: %s\n", tmpcut.Data());

  TH1D *hTRDEle=new TH1D("hTRDEle","", 200,0, 6); ll->Add(hTRDEle);
  const Double_t entTRDEle =ch->Draw("trdsig>>hTRDEle", tmpcut);
  //hTRDEle->Fit("gaus");
  //TF1 * fEle=hTRDEle->GetFunction("gaus"); ll->Add(fEle);

  TH1D *hTRDNclsEle=new TH1D("hTRDNclsEle","", (maxncls-minncls+2)*2, minncls-1, maxncls+1); ll->Add(hTRDNclsEle);
  ch->Draw("(trdncls+1e-10)/(trdnch+1e-10)>>hTRDNclsEle", tmpcut); 

  TH2D *hTRDSigNclsEle=new TH2D("hTRDSigNclsEle","", (maxncls-minncls+2)*2, minncls-1, maxncls+1, 200, 0, 6); ll->Add(hTRDSigNclsEle);
  ch->Draw("trdsig:(trdncls+1e-10)/(trdnch+1e-10)>>hTRDSigNclsEle", tmpcut);

  //--
  tmpcut = TRDcut+"&& pidV0==2";
  printf("tmpcut: %s\n", tmpcut.Data());

  TH1D *hTRDPi=new TH1D("hTRDPi","", 200,0, 6); ll->Add(hTRDPi);
  const Double_t entTRDPi = ch->Draw("trdsig>>hTRDPi", tmpcut);
  //hTRDPi->Fit("gaus");
  //TF1 * fpi=hTRDPi->GetFunction("gaus"); ll->Add(fpi);

  TH1D *hTRDNclsPi=new TH1D("hTRDNclsPi","", (maxncls-minncls+2)*2, minncls-1, maxncls+1); ll->Add(hTRDNclsPi);
  ch->Draw("(trdncls+1e-10)/(trdnch+1e-10)>>hTRDNclsPi", tmpcut); 

  TH2D *hTRDSigNclsPi=new TH2D("hTRDSigNclsPi","", (maxncls-minncls+2)*2, minncls-1, maxncls+1, 200, 0, 6); ll->Add(hTRDSigNclsPi);
  ch->Draw("trdsig:(trdncls+1e-10)/(trdnch+1e-10)>>hTRDSigNclsPi", tmpcut);

  //--

  tmpcut = TRDcut+"&& pidV0==4";
  printf("tmpcut: %s\n", tmpcut.Data());


  TH1D *hTRDpro=new TH1D("hTRDpro","", 200,0, 6); ll->Add(hTRDpro);
  const Double_t entTRDPro = ch->Draw("trdsig>>hTRDpro", tmpcut);
  //hTRDpro->Fit("gaus");
  //TF1 * fpro=hTRDpro->GetFunction("gaus"); ll->Add(fpro);

  TH1D *hTRDNclsPro=new TH1D("hTRDNclsPro","", (maxncls-minncls+2)*2, minncls-1, maxncls+1); ll->Add(hTRDNclsPro);
  ch->Draw("(trdncls+1e-10)/(trdnch+1e-10)>>hTRDNclsPro", tmpcut); 

  TH2D *hTRDSigNclsPro=new TH2D("hTRDSigNclsPro","", (maxncls-minncls+2)*2, minncls-1, maxncls+1, 200, 0, 6); ll->Add(hTRDSigNclsPro);
  ch->Draw("trdsig:(trdncls+1e-10)/(trdnch+1e-10)>>hTRDSigNclsPro", tmpcut);

  //--

  printf("entTRD Ele: %.0f pi: %.0f pro: %.0f\n", entTRDEle, entTRDPi, entTRDPro);



  //================================================================<<

#if 0
  //tpc
  const TString tpccut = TRDcut;

  printf("tpccut: %s\n", tpccut.Data());


  TH1D *htpcEle=new TH1D("htpcEle","", 200,0, 200); ll->Add(htpcEle);
  const Double_t enttpcEle =ch->Draw("tpcsig>>htpcEle", tpccut+"&& pidV0==1");
  //htpcEle->Fit("gaus");
  //TF1 * fEle=htpcEle->GetFunction("gaus"); ll->Add(fEle);

  TH1D *htpcpi=new TH1D("htpcpi","", 200,0, 200); ll->Add(htpcpi);
  const Double_t enttpcPi = ch->Draw("tpcsig>>htpcpi", tpccut+"&& pidV0==2");
  //htpcpi->Fit("gaus");
  //TF1 * fpi=htpcpi->GetFunction("gaus"); ll->Add(fpi);

  TH1D *htpcpro=new TH1D("htpcpro","", 200,0, 200); ll->Add(htpcpro);
  const Double_t enttpcPro = ch->Draw("tpcsig>>htpcpro", tpccut+"&& pidV0==4");
  //htpcpro->Fit("gaus");
  //TF1 * fpro=htpcpro->GetFunction("gaus"); ll->Add(fpro);

  printf("enttpc Ele: %.0f pi: %.0f pro: %.0f\n", enttpcEle, enttpcPi, enttpcPro);
#endif 


  //================================================================<<

  ilist=ilist(ilist.Last('/')+1, ilist.Length());
  ilist.Prepend(Form("outputneweff_mom%.1f_dp%.3f_nch%d_nclsNch%.3f_", mom, dp, nch, minncls));
  
  TFile *fout = new TFile(ilist,"recreate");
  ll->Write();
  fout->Save();
  fout->Close();

  delete fout;

  return;
}

int main(int argc, char * argv[])
{
  for(Int_t ii=0; ii<argc; ii++){
    printf("%d: %s\n", ii, argv[ii]);
  }

  if(argc!=3){
    printf("argc!=3\n"); return 1;
  }

  neweff(argv[1], atof(argv[2]));

  return 0;
}
